<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Edit_garage extends Page
{
       public $lblCity = null;
       public $Label13 = null;
       public $cmbCountry = null;
       public $edCity = null;
       public $Label11 = null;
       public $JSProvinceMunicipality = null;
       public $JSRegionProvince = null;
       public $Label10 = null;
       public $lblAddress = null;
       public $lblname = null;
       public $hfError = null;
       public $JSValidate = null;
       public $Label28 = null;
       public $cmbMunicipality = null;
       public $Label27 = null;
       public $cmbProvince = null;
       public $Label26 = null;
       public $cmbRegion = null;
       public $edBldg = null;
       public $Label25 = null;
       public $edAddress = null;
       public $Label24 = null;
       public $edname = null;
       public $hiddenContact = null;
       public $JTSiteTheme1 = null;
       public $Image2 = null;
       public $Label8 = null;
       public $btnContactCancel = null;
       public $btnSubmit = null;
       function edCityJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edCity','lblCity');
       <?php

       }

       function edAddressJSBlur($sender, $params)
       {

       ?>
       validateRequire('edAddress','lblAddress');
       <?php

       }
       function ednameJSBlur($sender, $params)
       {

       ?>
       validateRequire('edname','lblname');
       <?php

       }

       function validateRequired($value,$lbl)
       {
        if($value=='' || $value==null)
        {
         $this->$lbl->Caption = '&darr; This field is required &darr;';
         $this->hfError->setValue('1');
        }
       }

       function cmbProvinceJSChange($sender, $params)
       {
       //echo $this->cmbMunicipality->ajaxCall("VendorProvinceChange");
       ?>
       provinceMunicipality();
       return false;

       <?php

       }

       function cmbRegionJSChange($sender, $params)
       {
       //echo $this->cmbProvince->ajaxCall("VendorRegionchange");
       ?>
       regionProvince();
       return false;
       <?php

       }

       function btnSubmitJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       window.close()
       <?php

       }

       function btnSubmitClick($sender, $params)
       {
       $this->validateRequired($this->edname->Text,'lblname');
       $this->validateRequired($this->edAddress->Text, 'lblAddress');
       $this->validateRequired($this->edCity->Text, 'lblCity');
       $error = $this->hfError->getValue();
       if($error=='1' || $number!=$key)
        {
         $this->hfError->setValue('0');
         return false;
        }
       else if($error=='0')
        {

       $query_update = "UPDATE company_garage SET
       name ='".$this->edname->Text."',
       address = '".$this->edAddress->Text."',
       bldg = '".$this->edBldg->Text."',
       city = '".$this->edCity->Text."',
       country = '".$this->cmbCountry->getItemIndex()."'
       where auto_id='".$this->hiddenContact->getValue()."'";
       mysql_query($query_update) or die(mysql_error());

       echo '<script language=javascript>
       alert("Updated garage successfully");
       //alert(window.opener.location.href);
       window.opener.location.reload();
       window.close();
       </script>';

       }
       }




       function Edit_garageCreate($sender, $params)
       {
       $this->hfError->setValue('0');
       $this->lblname->Caption = '';
       $this->lblAddress->Caption = '';
       $this->lblCity->Caption = '';

       $branch = $_GET['auto_id'];
       $this->hiddenContact->setValue($branch);

       $query1 = "select * from company_garage where auto_id='".$this->hiddenContact->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());
       $row = mysql_fetch_array($result);


       $this->cmbCountry->Clear();
        $result_country = mysql_query("select * from countries order by country ASC");

        while($row_country = mysql_fetch_array($result_country))
        {
            $this->cmbCountry->AddItem($row_country['country'], null, $row_country['country']);
        }


       $this->edname->Text = $row['name'];
       $this->edAddress->Text = $row['street'];
       $this->edBldg->Text = $row['bldg'];
       $this->edCity->Text = $row['city'];
       $this->cmbCountry->ItemIndex = $row['country'];


        //Vendor Region,Prov, Municipality

       $this->cmbRegion->Clear();
       $this->cmbRegion->AddItem('--Select--', null, null);
       $this->cmbRegion->ItemIndex=$row['region'];
       $result_vend_region = mysql_query("select * from lookup_region");

        while($row_vend_region = mysql_fetch_array($result_vend_region))
        {
            $this->cmbRegion->AddItem($row_vend_region['region'], null, $row_vend_region['id']);
        }

       $region_id = $row['region'];
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       $province_id = $row['province'];
       $this->cmbMunicipality->Clear();
       $this->cmbMunicipality->AddItem('--Select--', null, null);
       $result_municipality = mysql_query("select * from lookup_municipality where province_id='".$province_id."'");

       while($row_municipality = mysql_fetch_array($result_municipality))
       {
            $this->cmbMunicipality->AddItem($row_municipality['municipality'], null, $row_municipality['id']);
       }
        $this->cmbProvince->setItemIndex($row['province']);
        $this->cmbMunicipality->setItemIndex($row['municipality']);


       }


       function VendorRegionChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       if($region_id==0)
       {
        $this->cmbProvince->Clear();
        $this->cmbMunicipality->Clear();
        $this->cmbProvince->AddItem('--Select Region First--',null,null);
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }


       function VendorProvinceChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       $province_id = $this->cmbProvince->getItemIndex();
       $this->cmbMunicipality->Clear();
       $this->cmbMunicipality->AddItem('--Select--', null, null);
       $result_municipality = mysql_query("select * from lookup_municipality where province_id='".$province_id."'");

       while($row_municipality = mysql_fetch_array($result_municipality))
       {
            $this->cmbMunicipality->AddItem($row_municipality['municipality'], null, $row_municipality['id']);
       }

       if($province_id==0)
       {
        $this->cmbMunicipality->Clear();
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }




       function btnContactCancelClick($sender, $params)
       {
       echo '<script language=javascript>
       window.close()</script>';


       }

}

global $application;

global $Edit_garage;

//Creates the form
$Edit_garage=new Edit_garage($application);

//Read from resource file
$Edit_garage->loadResource(__FILE__);

//Shows the form
$Edit_garage->show();

?>